<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>5.片元着色器粒子化图片</title>
    <style>
        canvas{
            border:1px solid #aaa;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
    <canvas width="1000" height="554"></canvas>
    <script src="/common/lib/gl-renderer.js"></script>

    <script type="module">
        import { loadImage } from "/common/lib/utils.js";

        (async function(){
            /**
             * gl-renderer 在 WebGL 底层的基础上进行了一些简单的封装，以便于我们将重点放在提供几何数据、设置变量和编写 Shader 上，不用因为创建 buffer 等细节而分心
             * 第一步: 创建 Renderer 对象
             * 第二步: 创建并启用 WebGL 程序
             * 第三步: 设置 uniform 变量
             * 第四步: 将顶点数据送入缓冲区
             * 第五步: 渲染
            */
            const canvas = document.querySelector('canvas'); 
            const renderer = new GlRenderer(canvas);

            // 第二步
            const vertex = `
                attribute vec2 a_vertexPosition;
                attribute vec2 uv;
                varying vec2 vUv;
                
                void main() {
                    gl_PointSize = 1.0;
                    vUv = uv;
                    gl_Position = vec4(a_vertexPosition, 1, 1);
                }
            `;
            const fragment = `
                #ifdef GL_ES
                precision mediump float;
                #endif

                uniform sampler2D tMap;
                uniform float uTime;
                varying vec2 vUv;

                float random(vec2 st){
                    return fract(sin(dot(st.xy,vec2(12.9898,78.233))) * 43758.5453123);
                }

                void main() {
                    vec2 st = vUv * vec2(100,55.4);
                    vec2 uv = vUv + 1.0 - 2.0 * random(floor(st));
                    vec4 color = texture2D(tMap, mix(uv, vUv, min(uTime, 1.0)));
                    gl_FragColor.rgb = color.rgb; 
                    gl_FragColor.a = color.a * uTime;
                }
            `;
            const program = renderer.compileSync(fragment,vertex);
            renderer.useProgram(program);
    
            // 第三步
            const texture = await renderer.loadTexture("https://p1.ssl.qhimg.com/t01cca5849c98837396.jpg");
            // const texture = await renderer.loadTexture("https://static001.geekbang.org/resource/image/20/cc/20febddf9e1edbb9e7bd349e544f24cc.jpg");
            renderer.uniforms.tMap = texture;
            renderer.uniforms.uTime = 0;
            

            // 第四步
            renderer.setMeshData([
                {
                    positions:[
                        [-1, -1], 
                        [-1, 1], 
                        [1, 1], 
                        [1, -1]
                    ],
                    attributes:{
                        uv:[
                            [0, 0], 
                            [0, 1], 
                            [1, 1], 
                            [1, 0],
                        ]
                    },
                    cells:[[0,1,2],[2,0,3]]
                }
            ])

            // 第五步
            renderer.render()

            // 时间变化
            function update(utime){
                let s = utime / 2000;
                renderer.uniforms.uTime = s;
                if(s <= 1) requestAnimationFrame(update);
            }
            update(0)
        })()

    </script>
</body>
</html>